<template>
  <el-dialog :title="title" v-model.visible="dialogVisible" width="600px"
    class="updateVersionDialog dialog-vertical-center dialog-body-padding-top-bottom-0" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="dialogClose">
    <el-form ref="dialogForm" v-loading="infoLoading" :model="fromRef" label-width="80px">
      <el-form-item label="描述" prop="description">
        <el-input v-model="fromRef.description" class="show-word-limit" type="textarea" :autosize="{ minRows: 2 }"
          placeholder="请输入描述" maxlength="255" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="dialogClose()">取消</el-button>
        <el-button type="primary" :loading="infoLoading || btnSaveLoading" @click="saveCommit">{{ buttonText
        }}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { nextTick, ref } from "vue";
import { ElMessage } from 'element-plus'
import { putWorkflows } from 'src/api/designerTool'
import { designerStore } from 'src/stores/designerTool/designerTool'
const store = designerStore()
const dialogVisible = ref(false)
const buttonText = ref('修改')
const title = ref('')
const btnSaveLoading = ref(false)
const infoLoading = ref(false)
const workflowId = ref('')
const fromRef = ref({ description: '' })
function init(row = {}) {
  workflowId.value = row.id
  getWorkflowInfo(row)
  dialogVisible.value = true
}
function getWorkflowInfo(item: any) {
  infoLoading.value = true
  title.value = `修改V${item.version}版本信息`
  fromRef.value.description = item.description
  infoLoading.value = false
}
const emits = defineEmits(['successFun', 'closeFun'])
function dialogClose() {
  dialogVisible.value = false
  nextTick(() => {
    emits('closeFun')
  })
}

function saveCommit() {
  const form = new FormData()
  form.append('description', fromRef.value.description)
  btnSaveLoading.value = true
  putWorkflows(store.project_id, workflowId.value, fromRef.value).then((res: any) => {
    ElMessage.success('修改版本信息成功')
    nextTick(() => {
      emits('successFun')
      dialogClose()
    })
  }).catch(error => {
    console.log(error)
  })
}
defineExpose({ init })
</script>

<style lang="scss" scoped>
.updateVersionDialog {}
</style>
